package service;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.jfinal.plugin.cron4j.ITask;
import common.model.Record;
import common.model.User;

import java.text.SimpleDateFormat;
import java.util.*;

public class TimingService implements ITask {

    String url="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2c6ac24d-9b6c-45fb-8fbb-63c9e6b3c558";

    @Override
    public void stop() {

    }

    @Override
    public void run() {
        System.out.println("定时任务被触发");
        Calendar calendar=Calendar.getInstance();
        String sql="select * from record where to_days(day) - to_days(now()) = 1";
        if(calendar.get(Calendar.DAY_OF_WEEK)==6){
            sql="select * from record where to_days(day) - to_days(now()) = 3";
        }
        List<Record> record = new Record().find(sql);
        if (record.size()<=0){
            return;
        }
        Map<String,List<Record>> records=new HashMap<>();
        Iterator<Record> iterator = record.iterator();
        while (iterator.hasNext()){
            Record item = iterator.next();
            String key = new User().findFirst("select * from user where name = '"+item.getSheying()+"'").getTel();
            if (!records.containsKey(key)){
                records.put(key,new ArrayList<>());
            }
            records.get(key).add(item);
        }
        sendWechatMessage(records);
    }

    public String getAtMessage(String tel){
        Map<String,Object> datas=new HashMap<>();
        datas.put("msgtype","text");
        Map<String,Object> text=new HashMap<>();
        text.put("content","您明天有如下录课(测试消息)");
        text.put("mentioned_mobile_list",new String[]{tel});
        datas.put("text",text);
        return JSONUtil.parse(datas).toString();
    }

    public String getNoticeMessage(List<Record> items){
        String content="# "+items.get(0).getSheying()+"的录课(测试消息)\n";
        for (Record item:items){
            content = content + "姓名:" + "<font color=\"warning\">"+item.getName()+"</font>\t";
            content+="节次:"+ "<font color=\"warning\">"+item.getJieci()+"</font>\n";
        }
        Map<String,Object> datas=new HashMap<>();
        datas.put("msgtype","markdown");
        Map<String,Object> markdown=new HashMap<>();
        markdown.put("content",content);
        datas.put("markdown",markdown);
        return JSONUtil.parse(datas).toString();
    }

    public void sendWechatMessage(Map<String,List<Record>> records){
        Set<String> keys = records.keySet();
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            System.out.println(key);
//            HttpRequest.post(url).body(JSONUtil.parse(getAtMessage(key)).toString()).execute();
//            HttpRequest.post(url).body(JSONUtil.parse(getNoticeMessage(records.get(key))).toString()).execute();
            //日志记录一下发送结果
            System.out.println(HttpRequest.post(url).body(JSONUtil.parse(getAtMessage(key)).toString()).execute().body());
//            System.out.println(HttpRequest.post(url).body(JSONUtil.parse(getNoticeMessage(records.get(key)))
//                    .toString()).execute().body());
        }
    }
}
